<?php
/**
 * Created by PhpStorm.
 * User: zhangrongxiang
 * Date: 2017/3/27
 * Time: 下午3:38
 */


//写一个函数，尽可能高效的，从一个标准url里取出文件的扩展名，
//例如:http://www.sina.com.cn/abc/de/fg.php?id=1需要取出php或.php（新浪）
// 方案一
function getExt1($url){
  $arr = parse_url($url);
  //Array ( [scheme] => http [host] => www.sina.com.cn [path] => /abc/de/fg.php [query] => id=1 )

  $file = basename($arr['path']);
  $ext = explode('.', $file);
  return $ext[count($ext)-1];
}

// 方案二
function getExt2($url){
  $url = basename($url);
  $pos1 = strpos($url,'.');
  $pos2 = strpos($url,'?');

  if (strstr($url,'?')) {
    return substr($url,$pos1+1,$pos2-$pos1-1);
  } else {
    return substr($url,$pos1);
  }

}

$path = "http://www.sina.com.cn/abc/de/fg.php?id=1";
echo getExt1($path);
echo "<br />";
echo getExt2($path);


print_r(parse_url($path));
print_r(basename($path));